package demo.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import demo.common.Result;
import demo.entity.Policy;
import demo.mapper.PolicyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@CrossOrigin(maxAge = 3600)
@RestController
@RequestMapping("/policy")
public class PolicyController {
  @Resource
  PolicyMapper policyMapper;

  @PostMapping
  public Result<?> save(@RequestBody Policy policy){
    policyMapper.insert(policy);
    return Result.success();
  }
  @GetMapping
  public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                            @RequestParam(defaultValue = "10") Integer pageSize,
                            @RequestParam(defaultValue = "") String search) {
    LambdaQueryWrapper<Policy> wrapper = Wrappers.<Policy>lambdaQuery();
    if (StrUtil.isNotBlank(search)) {
      wrapper.like(Policy::getPolname, search);
    }
    Page<Policy> userPage = policyMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
    return Result.success(userPage);
  }

}
